package com.yuyou.fn.educational.service.impl;

import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.educational.service.IPeriodSegmentService;
import com.yuyou.fn.educational.vo.PeriodSaveOrUpdateVo;
import com.yuyou.fn.educational.vo.PeriodSegmentVo;
import com.yuyou.fn.educational.vo.PeriodVo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.yuyou.fn.educational.mapper.PeriodMapper;
import com.yuyou.fn.educational.entity.Period;
import com.yuyou.fn.educational.service.IPeriodService;
import com.github.wens.framework.service.impl.SuperServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 * Period 表数据服务层接口实现类
 *
 */
@Service
public class PeriodServiceImpl extends SuperServiceImpl<PeriodMapper, Period,Long> implements IPeriodService {

    @Resource
    private IPeriodSegmentService periodSegmentService ;

    @Cacheable("CacheOfPeriodList")
    @Override
    public List<Period> findAllFromCache() {
        Example<Period> example = Period.createExample();
        return findListByExample(example);
    }

    @Transactional
    @CacheEvict(value = "CacheOfPeriodList",allEntries = true)
    @Override
    public void saveOrUpdatePeriod(PeriodSaveOrUpdateVo periodSaveOrUpdateVo, List<PeriodSegmentVo> segmentList) {

        Period period = new Period();
        BeanUtils.copyProperties(period,periodSaveOrUpdateVo);
        checkPeriod(period , segmentList );
        period.setStartDate(segmentList.get(0).getStartDate());
        period.setEndDate(segmentList.get(segmentList.size() - 1).getEndDate());

        if(period.getPeriodId() == null ){
            period.setUpdateTime(TimeUtils.now());
            period.setCreateTime(TimeUtils.now());
            insertSelective(period);
        }else{
            period.setUpdateTime(TimeUtils.now());
            updateSelectiveById(period);
        }

        periodSegmentService.updateSegment(period.getPeriodId(),segmentList);
    }

    private void checkPeriod(Period period, List<PeriodSegmentVo> segmentList) {
        Example<Period> example = Period.createExample();
        example.createCriteria().andEqualTo("periodNo",period.getPeriodNo());
        Period one = findOneByExample(example);
        if(one != null && ( period.getPeriodId() == null || !period.getPeriodId().equals(one.getPeriodId()) )){
            throw new BusException("期次重复");
        }

        for(int i = 0 ; i < segmentList.size() ;i++ ){
            PeriodSegmentVo vo = segmentList.get(i);
            if(vo.getStartDate().after(vo.getEndDate())){
                throw new BusException("段次" + (i + 1 ) + "开始日期大于结束日期") ;
            }
            if(i > 0 &&   vo.getStartDate().before(segmentList.get(i - 1).getEndDate())){
                throw new BusException("段次" + (i + 1 ) + "开始日期小于段次"+(i - 1)+"结束日期") ;
            }
        }
    }

    @Override
    public PeriodVo findPeriodByPeriodId(Long periodId) {
        Period period = findById(periodId);
        PeriodVo periodVo  = new PeriodVo() ;
        BeanUtils.copyProperties(periodVo,period);
        List<PeriodSegmentVo> segmentList = periodSegmentService.findPeriodSegmentList(periodId);
        periodVo.setSegmentList(segmentList);
        return periodVo ;


    }

    @Override
    public Page<Period> findPeriodForPage(int pageNo, int pageSize) {
        Example<Period> example = Period.createExample();
        example.orderBy("periodNo").desc() ;
        return findPageByExample(example,pageNo,pageSize);

    }
}